package sourceCode;

public class GraphePoid {

    private int[][] arretes;
    private Object[] noeuds;

    public GraphePoid(int n) {
        arretes = new int[n][n];
        noeuds = new Object[n];
    }


    public int size() {
        return noeuds.length;
    }

    public void setNoeud(int sommet, Object noeud) {
        noeuds[sommet] = noeud;
    }

    public Object getNoeud(int sommet) {
        return noeuds[sommet];
    }

    public int getIdNoeud(Object noeud) {
        for (int i = 0; i < noeuds.length; i++) {
            if (noeud == noeuds[i])
                return i;
        }
        return -1;
    }


    public void ajouterArrete(int source, int destination, int poids) {
        arretes[source][destination] = poids;
    }

    public boolean estArrete(int source, int destination) {
        return arretes[source][destination] > 0;
    }

    public void retirerArrete(int source, int destination) {
        arretes[source][destination] = 0;
    }

    public int getPoids(int source, int destination) {
        return arretes[source][destination];
    }

    public int[] voisins(int sommet) {
        int compteur = 0;
        for (int i = 0; i < arretes[sommet].length; i++) {
            if (arretes[sommet][i] > 0)
                compteur++;
        }
        final int[] reponse = new int[compteur];
        compteur = 0;
        for (int i = 0; i < arretes[sommet].length; i++) {
            if (arretes[sommet][i] > 0)
                reponse[compteur++] = i;
        }
        return reponse;
    }

    public void print() {
        for (int j = 0; j < arretes.length; j++) {
            System.out.print(noeuds[j] + ": ");
            for (int i = 0; i < arretes[j].length; i++) {
                if (arretes[j][i] > 0)
                    System.out.print(noeuds[i] + ":" + arretes[j][i] + " ");
            }
            System.out.println();
        }
    }


}

